--[[
	Project....: LUI NextGenWoWUserInterface - BuffWatcher Module
	File.......: buffwatcher.lua
	Description: Buff Watcher Module
	Version....: 1.0
	Rev Date...: 29/11/2010
]]
local LUI = LibStub("AceAddon-3.0"):GetAddon("LUI")
local LSM = LibStub("LibSharedMedia-3.0")
local widgetLists = AceGUIWidgetLSMlists
local LUIHook = LUI:GetModule("LUIHook")
local module = LUI:NewModule("BuffWatcher", "AceHook-3.0")
local db

local mainFrame = nil;

local defaults = {
	BuffWatcher = {
		Enable = false,
		BuffWidth = 30,
		BuffHeight = 30,
		Spells = {
			["mage"] = {
				{
					name = "Focus Magic",
					requires = "group"
				},
				{
					name = "Molten Armor"
				},
				{
					name = "Dalaran Brilliance"
				}
			},
			["druid"] = {
				{
					name = "Mark of the Wild"
				}
			},
		}
	}
};

function module:LoadOptions()
	local options = {
		BuffWatcher = {
			name = "Buff Watcher",
			type = "group",
			order = 60,
			disabled = function() return not db.BuffWatcher.Enable; end,
			childGroups = "select",
			args = {
				empty1 = {
					name = " \n ",
					width = "full",
					type = "description",
					order = 1
				},
				desc = {
					name = "|cff3399ffNotice:|r\nOptions will be added later.",
					width = "full",
					type = "description",
					order = 2
				}
			}
		}
	};
	return options
end

function module:LoadModule()
	local module = {
		BuffWatcher = {
			order = LUI:GetModuleCount(),
			type = "execute",
			name = function()
				if (db.BuffWatcher.Enable) then
					return "|cff00ff00Buff Watcher Enabled|r";
				else
					return "|cffFF0000Buff Watcher Disabled|r";
				end
			end,
			func = function(self, BW)
				db.BuffWatcher.Enable = not db.BuffWatcher.Enable;
				StaticPopup_Show("RELOAD_UI");
			end
		}
	};
	return module;
end

function module:OnInitialize()
	LUI:MergeDefaults(LUI.db.defaults.profile, defaults);
	LUI:RefreshDefaults();
	LUI:Refresh();
	self.db = LUI.db.profile;
	db = self.db;
end

function module:OnEnable()
	LUI:RegisterOptions(self:LoadOptions());
	LUI:RegisterModule(self:LoadModule());
	if (db.BuffWatcher.Enable) then
		self:CreateMainFrame();
	end
end

local print = function(msg)
	DEFAULT_CHAT_FRAME:AddMessage(msg);
end

local pairs = pairs;
function module:UNIT_AURA(unit)
	if (unit ~= "player") then
		return;
	end
	for _,v in pairs(db.BuffWatcher.Spells) do
		if (not v.requires or v.requires ~= "group") then
			v.found = false;
			v.button:SetAlpha(1);
		end
	end
	local buffs, i = {}, 1;
	local buff = UnitBuff("player", i);
	while buff do
		for _,v in pairs(db.BuffWatcher.Spells) do
			if (v.name == buff) then
				v.found = true;
				v.button:SetAlpha(0);
				break;
			end
		end
		i = i + 1
		buff = UnitBuff("player", i);
	end
end
function module:PLAYER_REGEN_DISABLED(...)
	for _,v in pairs(db.BuffWatcher.Spells) do
		local process = true;
		if (not v.found) then
			if (v.requires) then
				if (v.requires == "group") then
					if (not UnitInParty("player") or not UnitInRaid("player")) then
						process = false;
					end
				end
			end
			if (process == true) then
				v.button:SetAlpha(1);
				print("Buff '" .. v.name .. "' not found!");
			end
		end
	end
end

-- Unit_Aura_Gained

function StopMoving(self)
	self:StopMovingOrSizing();
end
function module:CreateButton(spellInfo, ctr)
	local b = CreateFrame("Button", "LUIBuffWatcher_Button"..spellInfo.spellID, self.frame, "SecureActionButtonTemplate");
	b:SetWidth(db.BuffWatcher.BuffWidth);
	b:SetHeight(db.BuffWatcher.BuffHeight);
	b:SetPoint("LEFT", self.frame, "LEFT", (ctr * db.BuffWatcher.BuffWidth)+5, 0);
	b:SetNormalTexture(spellInfo.icon);
	b:SetAttribute("type", "spell");
	b:SetAttribute("spell", spellInfo.name);
	b:SetAttribute("unit", "player");
	b:Show();
	return b;
end
function module:UNIT_SPELLCAST_SUCCEEDED(unit, spell, rank, unknwn, spellId)
	for _, i in pairs(db.BuffWatcher.Spells) do
		if (i.name == spell) then
			if (i.requires and i.requires == "group") then
				i.button:SetAlpha(0);
				i.found = true;
			end
		end
	end			
end
function module:CreateMainFrame()
	if (mainFrame ~= nil) then
		return;
	end
	mainFrame = CreateFrame("Frame", "LUIBuffWatcherFrame", UIParent);
	mainFrame:RegisterEvent("PLAYER_REGEN_DISABLED");
	mainFrame:RegisterEvent("UNIT_AURA");
	mainFrame:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED");
	mainFrame:SetWidth(((#db.BuffWatcher.Spells) * db.BuffWatcher.BuffWidth) + 10);
	mainFrame:SetHeight(db.BuffWatcher.BuffHeight + 10);
	mainFrame:SetBackdrop({
		bgFile = LSM:Fetch("background", "Blizzard Tooltip"),
		edgeFile = LSM:Fetch("border", "Stripped_medium"),
		edgeSize = 16,
		tile = false,
		tileSize = 0,
		insets = {
			top = 5,
			right = 5,
			bottom = 5,
			left = 5
		}
	});
	mainFrame:SetPoint("LEFT", UIParent, "LEFT", 0, 0);
	mainFrame:SetPoint("BOTTOM", UIParent, "BOTTOM", 0, 250);
	mainFrame:EnableMouse(true);
	mainFrame:SetMovable(true);
	mainFrame:SetToplevel(true);
	mainFrame:SetFrameStrata("HIGH");
	mainFrame:SetFrameLevel(20);
	mainFrame:SetScript("OnMouseDown", mainFrame.StartMoving);
	mainFrame:SetScript("OnMouseUp", StopMoving);
	self.frame = mainFrame;
	local counter = 0;
	for i = 1, MAX_SKILLLINE_TABS do
		local name, texture, offset, numSpells = GetSpellTabInfo(i);
		if (not name) then
			break;
		end
		for s = offset + 1, offset + numSpells do
			local spell, rank = GetSpellBookItemName(s, BOOKTYPE_SPELL);
			for _, v in pairs(db.BuffWatcher.Spells) do
				if (spell == v.name) then
					v.icon = GetSpellTexture(spell);
					v.spellID = s;
					v.button = self:CreateButton(v, counter);
					counter=counter+1;
					break;
				end
			end
		end
	end

	mainFrame:Show();

	mainFrame:SetScript("OnEvent", function(this, event, ...) module[event](this, ...); end);
end